using System;
using System.Collections;
using System.Reflection;

// ------    Para Usar esta Clase   --------------

//	ComboBox combo = new ComboBox();
//	combo.DataSource = EnumHelper.ToList(typeof(SimpleEnum));
//	combo.DisplayMember = "Value";
//	combo.ValueMember = "Key";

namespace Common
{
    /// <summary>
    /// Provides a description for an enumerated type.
    /// </summary>
    [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field,
         AllowMultiple = false)]
    public sealed class EnumDescriptionAttribute : Attribute
    {
        private string description;

        /// <summary>
        /// Gets the description stored in this attribute.
        /// </summary>
        /// <value>The description stored in the attribute.</value>
        public string Description
        {
            get
            {
                return this.description;
            }
        }

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="EnumDescriptionAttribute"/> class.
        /// </summary>
        /// <param name="description">The description to store in this attribute.
        /// </param>
        public EnumDescriptionAttribute(string description)
            : base()
        {
            this.description = description;
        }
    }

    /// <summary>
    /// Provides a static utility object of methods and properties to interact
    /// with enumerated types.
    /// </summary>
    //public static class EnumHelper
    public class EnumHelper
    {

        public static string GetDescription(Enum value)
        {
            //			if (value == null)
            //			{
            //				throw new ArgumentNullException("value");
            //			}

            string description = value.ToString();
            FieldInfo fieldInfo = value.GetType().GetField(description);
            EnumDescriptionAttribute[] attributes =
                (EnumDescriptionAttribute[])
                fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            return description;
        }

        public static string GetValueByDescription(Type type, string DescriptionValue)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            Array enumValues = Enum.GetValues(type);

            foreach (Enum value in enumValues)
            {
                if (GetDescription(value) == DescriptionValue)
                {
                    return value.ToString();
                }
            }
            return string.Empty;
        }

        public static object GetEnumValueByDescription(Type type, string DescriptionValue)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            Array enumValues = Enum.GetValues(type);

            foreach (Enum value in enumValues)
            {
                if (GetDescription(value) == DescriptionValue)
                {
                    return value;
                }
            }

            return null;
        }

        /// <summary>
        /// Converts the <see cref="Enum" /> type to an <see cref="IList" /> 
        /// compatible object.
        /// </summary>
        /// <param name="type">The <see cref="Enum"/> type.</param>
        /// <returns>An <see cref="IList"/> containing the enumerated
        /// type value and description.</returns>
        public static IList ToList(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            ArrayList list = new ArrayList();
            Array enumValues = Enum.GetValues(type);

            foreach (Enum value in enumValues)
            {
                //list.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
                DictionaryEntry obj = new DictionaryEntry(value, GetDescription(value));
                list.Add(obj);
            }

            return list;
        }

        /// <summary>
        /// Similar al metodo ToList, Pero Extrae el Value del atributo Descripcion del Enum.
        /// </summary>
        /// <param name="type">The <see cref="Enum"/> type.</param>
        /// <returns>An <see cref="IList"/> containing the enumerated
        /// type value and description.</returns>
        public static IList ToListConValorEnDescripcion(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            ArrayList list = new ArrayList();
            Array enumValues = Enum.GetValues(type);

            foreach (Enum value in enumValues)
            {
                DictionaryEntry obj = new DictionaryEntry(GetDescription(value), value.ToString());
                list.Add(obj);
            }

            return list;
        }
    }
}
